/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package listener;

/**
 *
 * @author Alexx
 */

import engine.Singleton;
import figure.Cross;
import figure.Round;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import json.JsonSerializer;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import server.ConnectClient;
import tictactoe.BeforeGame;
import tictactoe.EndGame;
import tictactoe.Offer;
import tictactoe.SmallField;
import tictactoe.UserComman;
import tictactoe.noGame;
import org.apache.log4j.Logger;

public class EventProducer {
 // ConnectClient connectClient;
  private static final Logger log = Logger.getLogger(EventProducer.class);
  private ArrayList<MyListener> listeners = new ArrayList<MyListener>();
 
//  public EventProducer (ConnectClient connectClient)
//  {
//    this.connectClient = connectClient;
//  }
  
  public void addMyListener(MyListener listener)
  {
    listeners.add(listener);
  }
  
    public String getClient(String str) throws org.json.simple.parser.ParseException
   {
       String type;
       String rety = str;
      // MyListener control = new ClientController(connectClient);
       
       JsonSerializer json = new JsonSerializer();
       json.startGet(str);
       
       type = json.getString("Type");
       
       if (type.equals("Users"))
       {
         String name = json.getString("Name");
         log.debug(name);
         rety = "Users";
         try {
             MyEvent e = new MyEvent(name);
             for(MyListener listener : listeners){
                 listener.addUser(e);
             }
         } catch (Exception e)
        {log.info("test" + e);}
       }
       
       if (type.equals("Offer"))
       {
         String name = json.getString("Name");
         log.debug(name);
         rety = "Offer";
         try {
             MyEvent e = new MyEvent(name);
             for(MyListener listener : listeners){
                 listener.offerUser(e);
             }
         } catch (Exception e)
        {log.info("test" + e);}
       }
       
       if (type.equals("NoGame"))
       {
         rety = "NoGame";
         try {
             MyEvent e = new MyEvent();
             for(MyListener listener : listeners){
                 listener.noGame(e);
             }
         } catch (Exception e)
        {log.info("test" + e);}
       }
       
       if (type.equals("Wait"))
       {
         rety = "Wait";
        // UserComman.waiter();
       }
       
       if (type.equals("StartGame"))
       {
         String figure = json.getString("Figure");
         String step = json.getString("Step");
         rety = "StartGame";
         try {
             MyEvent e = new MyEvent(figure, step);
             for(MyListener listener : listeners){
                 listener.startGame(e);
             }
         } catch (Exception e)
        {log.info("test" + e);}
       }
       
       if (type.equals("Step"))
       {
         String row = json.getString("Row");
         String col = json.getString("Col");
         rety = "Step";
         try {
             MyEvent e = new MyEvent(row, col);
             for(MyListener listener : listeners){
                 listener.partnerStep(e);
             }
         } catch (Exception e)
        {log.info("test" + e);}
       }
       
       if (type.equals("Finish"))
       {
         String status = json.getString("Status");
         rety = "Finish";
         try {
             MyEvent e = new MyEvent(status);
             for(MyListener listener : listeners){
                 listener.finishGame(e);
             }
         } catch (Exception e)
        {log.info("test" + e);}
       }
       
       return rety;
   }
  
}
